/*
 * Pratama Nur Wijaya (c) 2013 
 * 
 * Project       : OngkirJNE
 * Filename      : Fr_Hasil.java
 * Creation Date : May 17, 2013 time : 5:19:57 PM
 *
 */

package id.pratama.ongkirjne.fragment;

import org.json.JSONArray;
import org.json.JSONObject;

import id.pratama.ongkirjne.OngkirRestClient;
import id.pratama.ongkirjne.R;
import id.pratama.ongkirjne.Utils;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class Fr_Hasil extends SherlockFragment
{

	private static final String	TAG			= Fr_Hasil.class.getName();

	private final static String	API_KEY		= "2b97fb5df15c562a5945ce47867e1c6e";
	private final static String	COURIER		= "jne";
	private final static String	FORMAT_JSON	= "json";

	private static String		TAG_API_KEY	= "API-Key";
	private static String		TAG_FROM	= "from";
	private static String		TAG_TO		= "to";
	private static String		TAG_WEIGHT	= "weight";
	private static String		TAG_COURIER	= "courier";
	private static String		TAG_FORMAT	= "format";

	private Bundle				b;
	private ProgressDialog		pDialog;

	private TableLayout			table;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		Utils.TRACE(TAG, "oncreateview");
		if (savedInstanceState != null)
		{
			b = savedInstanceState.getBundle("bundle");
		}
		return inflater.inflate(R.layout.fr_hasil, container, false);
	}

	@Override
	public void onStart()
	{
		Utils.TRACE(TAG, "onstart");
		super.onStart();
		b = getArguments();
		if (b != null)
		{
			Utils.TRACE(TAG, "ambil bundle");
			Utils.TRACE(TAG, b.getString(Fr_Input.TAG_ASAL));
			updateHasil(b);
		}
	}

	public void updateHasil(Bundle b)
	{
		Utils.TRACE(TAG, "updatehasil");
		TextView asal = (TextView) getActivity().findViewById(R.id.kotaAsal);
		TextView tujuan = (TextView) getActivity().findViewById(R.id.kotaTujuan);
		asal.setText(b.getString(Fr_Input.TAG_ASAL));
		tujuan.setText(b.getString(Fr_Input.TAG_TUJUAN));

		RequestParams params = new RequestParams();
		params.put(TAG_API_KEY, API_KEY);
		params.put(TAG_FROM, b.getString(Fr_Input.TAG_ASAL));
		params.put(TAG_TO, b.getString(Fr_Input.TAG_TUJUAN));
		params.put(TAG_WEIGHT, b.getString(Fr_Input.TAG_BERAT));
		params.put(TAG_COURIER, COURIER);
		params.put(TAG_FORMAT, FORMAT_JSON);

		table = (TableLayout) getActivity().findViewById(R.id.tableHasil);

		getData(params);

	}

	@Override
	public void onSaveInstanceState(Bundle outState)
	{
		Utils.TRACE(TAG, "onsaveinstacestate");
		super.onSaveInstanceState(outState);
		outState.putBundle("bundle", b);
	}

	private void getData(RequestParams params)
	{
		Utils.TRACE(TAG, "try request json to API ONGKIR");
		OngkirRestClient.post(params, new JsonHttpResponseHandler()
		{
			@Override
			public void onStart()
			{

				super.onStart();
				pDialog = new ProgressDialog(getActivity());
				pDialog.setMessage("Loading...");
				pDialog.setCancelable(true);
				pDialog.show();
			}

			@Override
			public void onFinish()
			{

				super.onFinish();
				pDialog.dismiss();
			}

			@Override
			public void onSuccess(int arg0, JSONObject jsonObject)
			{
				super.onSuccess(arg0, jsonObject);
				try
				{
					JSONArray listHarga = jsonObject.getJSONArray("price");
					if (listHarga.length() > 0)
					{
						for (int i = 0; i < listHarga.length(); i++)
						{
							JSONObject hasil = listHarga.getJSONObject(i);
							TableRow tr = new TableRow(getActivity());
							tr.setLayoutParams(new
									TableRow.LayoutParams(LayoutParams.MATCH_PARENT,
											LayoutParams.MATCH_PARENT));
							tr.setBackgroundResource(R.drawable.cell_shape);

							TableRow.LayoutParams rowParams = new TableRow.LayoutParams(
									TableRow.LayoutParams.MATCH_PARENT,
									TableRow.LayoutParams.WRAP_CONTENT, 1f);

							TextView textService = new
									TextView(getActivity());
							textService.setText(hasil.getString("service"));
							textService.setTextSize(getActivity().getResources().getDimension(R.dimen.textSize));
							textService.setWidth(0);
							textService.setPadding(0, 0, 30, 0);
							textService.setTextColor(Color.BLACK);
							textService.setLayoutParams(rowParams);

							TextView textValue = new
									TextView(getActivity());
							textValue.setText("Rp. " + hasil.getString("value"));
							textValue.setTextSize(getActivity().getResources().getDimension(R.dimen.textSize));
							textValue.setLayoutParams(rowParams);
							textValue.setTextColor(Color.BLACK);

							tr.addView(textService);
							tr.addView(textValue);

							table.addView(tr, new
									TableLayout.LayoutParams(LayoutParams.MATCH_PARENT,
											LayoutParams.MATCH_PARENT));

						}
					}
				} catch (Exception e)
				{
					Utils.TRACE(TAG, e.getMessage());
				}
			}
		});

	}
}
